/**
  ******************************************************************************
  * @file 	zbProtocol.h 
  * @brief  Protocol header file 
  ******************************************************************************
  
LITTLE ENDIAN !

Name					Size (bytes)	Description
Start of packet / ID	1			0x01 (SOH) default
Transaction Number		1			repeats master
Command					1			See command bytes and their arguments below
Flags					1			Flags to show status of packet, see below definition
Size					2			first size of packet payload 
Payload					Variable	Payload or command arguments
- offset				2			offset in memory from g_vars
- size					2			size to read or write
-- data					Var			in a write this holds the data of size
CRC						2			CRC of entire packet, including header

Flags 
0 - error
1 - ack
2 - vid data ? 
*/
#ifndef __ZBPROTOCOL_H
#define __ZBPROTOCOL_H

#include "main.h"


// commands
#define cmd_read 0x00
#define cmd_write 0x01
#define cmd_rw 0x02

#define FLAG_VIDEO 0x01

#define cmd_last = cmd_write;

// defines
#define hdrSize			6
#define	crcSize			4
#define maxPayloadSize 	512
#define rxMaxPacketSize (g_VarsSize + hdrSize + crcSize)
#define pktBuffSize 	(maxPayloadSize + hdrSize + crcSize)*2 // 

typedef struct{
	u8	ERR;
	u16 SIZE; // size to wait for
}pktErr;
	


// global vars
extern u8 g_PktTXBuff[pktBuffSize];
extern u8 dataState;
extern u32 botStatus;
// buffer with whole frame 


extern u32 pktTimeoutAt;
extern u32 idleTimeoutAt;

// file scope vars 
static u8 	ID 			= 0xBB;
 

// prototypes
u8 createHeader(u8 headerLocation[], u8 TXN, u8 command, u8 flags, u16 size );	// create header
void appendCRC(u8 dataBuffer[], u16 size); // append crc after size
void putPacketInBuffer(u8 payload[], u8 TXN, u8 command, u8 flags, u16 size); // pointless now ? 
void checkPacket(CircularBuffer *cb, pktErr *err);
u16 processPacket(void);
u16 createResponse(u8 packet[], u16 payloadSize, u8 flags);
void initProtocol(void);

#endif
